package cn.com.shopec.erp.purchase.service.imp;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.service.IProductStockService;
import cn.com.shopec.erp.purchase.dao.IStockPurchaseDao;
import cn.com.shopec.erp.purchase.entity.StockPurchase;
import cn.com.shopec.erp.purchase.service.IStockPurchaseService;

/**
 * @ClassName: StockPurchaseServiceImpl
 * @Description: 采购单关联货品仓库service实现
 * @author: liang shun wang
 * @date 2013-5-22 上午11:06:35
 * 
 */
@Service
public class StockPurchaseServiceImpl implements IStockPurchaseService {

	@Resource
	private IStockPurchaseDao stockPurchaseDao;

	@Resource
	private IStockPurchaseDao istockPurchaseDao;

	@Resource
	private IProductStockService iproductStockService;

	/**
	 * @methodName：addPurchaseStock
	 * @Description: 添加入库信息
	 * @param StockInProductVo
	 * @return ModelMap
	 * @author: liang shun wang
	 * @throws Exception
	 * @date: 2013-5-21 下午18:22:03
	 */
	@Override
	@Transactional
	public boolean addPurchaseStock(StockPurchase stockPurchase) throws Exception {
		boolean bool = false;
		if (stockPurchase != null) {
			stockPurchase.setCreateTime(DateUtil.formatTimesTampDate(new Date()));
			stockPurchaseDao.save(stockPurchase);
			bool = true;
		}
		return bool;
	}

	/**
	 * @Description: 从创建时间最久的开始扣
	 * @param StockInProductVo
	 * @return 1.库存id 2.需要减少的数量
	 * @author: liang shun wang
	 * @throws Exception
	 * @date: 2013-5-21 下午18:22:03
	 */
	@Override
	@Transactional
	public boolean updatePurchaseStockLastNumber(String id, int number) throws Exception {
		boolean bool = false;
		StockPurchase stockPurchase = getProductPurchaseStock(id);
		if (stockPurchase != null) {
			StockPurchase stock = stockPurchaseDao.get("id", id);
			stock.setRemainingAmount(number);
			bool = true;
		}
		return bool;
	}

	/**
	 * @Description: 从创建时间最久的开始扣
	 * @param StockInProductVo
	 * @return 1.库存id 2.需要减少的数量
	 * @author: liang shun wang
	 * @throws Exception
	 * @date: 2013-5-21 下午18:22:03
	 */
	@Override
	@Transactional
	public boolean updatePurchaseStockLastNumber(String id, String poNo, int number) throws Exception {
		boolean bool = false;
		StockPurchase stockPurchase = getProductPoNoStock(id, poNo);
		if (stockPurchase != null) {
			StockPurchase stock = stockPurchaseDao.getStockPurchaseByPoNo(id, poNo);
			stock.setCommonNumner(number);
			stockPurchaseDao.update(stock);
			bool = true;
		}
		return bool;
	}

	/**
	 * 根据货品仓库id查询所对应的数据
	 */
	@Override
	public StockPurchase getProductPurchaseStock(String id) {
		DetachedCriteria criteria = istockPurchaseDao.createCriteria();
		criteria.add(Restrictions.eq("id", id));
		List<StockPurchase> lists = stockPurchaseDao.findByCriteria(criteria);
		if (null != lists && lists.size() > 0) {
			return lists.get(0);
		}
		return null;
	}

	/**
	 * 根据货品仓库id和采购单号查询所对应的数据
	 */
	public StockPurchase getProductPoNoStock(String id, String poNo) {
		DetachedCriteria criteria = istockPurchaseDao.createCriteria();
		if (id != null) {
			criteria.add(Restrictions.eq("id", id));
		}

		if (poNo != null) {
			criteria.add(Restrictions.eq("poNo", poNo));
		}
		List<StockPurchase> lists = stockPurchaseDao.findByCriteria(criteria);
		if (null != lists && lists.size() > 0) {
			return lists.get(0);
		}
		return null;
	}

	public StockPurchase getProductPoNoStockNo(String stockNo, String poNo) {
		if (stockNo != null && poNo != null) {
			DetachedCriteria criteria = istockPurchaseDao.createCriteria();
			if (stockNo != null) {
				criteria.add(Restrictions.eq("stockNo", stockNo));
			}
			if (poNo != null) {
				criteria.add(Restrictions.eq("poNo", poNo));
			}
			List<StockPurchase> lists = stockPurchaseDao.findByCriteria(criteria);
			if (null != lists && lists.size() > 0) {
				return lists.get(0);
			}
		}
		return null;
	}

	@Override
	public StockPurchase getProductPoNo(StockPurchase stockPurchase) {
		DetachedCriteria criteria = istockPurchaseDao.createCriteria();
		if (!StringUtil.isBlank(stockPurchase.getPoNo())) {
			criteria.add(Restrictions.ilike("poNo", stockPurchase.getPoNo().trim(), MatchMode.ANYWHERE));
		}
		if (!StringUtil.isBlank(stockPurchase.getProdNo())) {
			criteria.add(Restrictions.eq("prodNo", stockPurchase.getProdNo().trim()));
		}
		if (!StringUtil.isBlank(stockPurchase.getWhNo())) {
			criteria.add(Restrictions.eq("whNo", stockPurchase.getWhNo().trim()));
		}
		if (!StringUtil.isBlank(stockPurchase.getPrqNo())) {
			criteria.add(Restrictions.ilike("prqNo", stockPurchase.getPrqNo().trim(), MatchMode.ANYWHERE));
		}
		
		List<StockPurchase> lists = stockPurchaseDao.findByCriteria(criteria);
		if (null != lists && lists.size() > 0) {
			return lists.get(0);
		}
		return null;
	}

	@Override
	public void updateStockPurchase(StockPurchase stockPurchase) throws Exception {
		stockPurchaseDao.update(stockPurchase);
	}

	@Override
	public PageFinder<StockPurchase> queryStockPurchaseForDefective(StockPurchase stockPurchase, PageQuery pageQuery) {
		DetachedCriteria detachedCriteria = getQueryCriteria(stockPurchase);
		detachedCriteria.add(Restrictions.gt("yieldAmountNumner", 0L));// 良品数大于0
		detachedCriteria.addOrder(Order.desc("prodNo"));
		detachedCriteria.addOrder(Order.desc("commoNo"));
		detachedCriteria.addOrder(Order.desc("poNo"));
		return stockPurchaseDao.pagedByCriteria(detachedCriteria, pageQuery.getPage(), pageQuery.getPageSize());
	}
	
	@Override
	public PageFinder<StockPurchase> queryStockPurchaseForQualified(StockPurchase stockPurchase, PageQuery pageQuery) {
		DetachedCriteria detachedCriteria = getQueryCriteria(stockPurchase);
		detachedCriteria.add(Restrictions.gt("yieldAmountNumner", 0L));// 良品数大于0
		detachedCriteria.addOrder(Order.asc("createTime"));
		detachedCriteria.addOrder(Order.desc("poNo"));
		return stockPurchaseDao.pagedByCriteria(detachedCriteria, pageQuery.getPage(), pageQuery.getPageSize());
	}

	/**
	 * 设置查询入库信息的条件
	 *
	 * @param stockPurchase
	 * @return
	 */
	private DetachedCriteria getQueryCriteria(StockPurchase stockPurchase) {
		DetachedCriteria detachedCriteria = null;
		detachedCriteria = stockPurchaseDao.createCriteria();
		if (StringUtil.isExist(stockPurchase.getProdNo())) {// 货品编号
			detachedCriteria.add(Restrictions.like("prodNo", stockPurchase.getProdNo()));
		}
		if (StringUtil.isExist(stockPurchase.getCommoNo())) {// 商品编号
			detachedCriteria.add(Restrictions.like("commoNo", stockPurchase.getCommoNo()));
		}
		if (StringUtil.isExist(stockPurchase.getCommoName())) {// 商品名称
			detachedCriteria.add(Restrictions.like("commoName", stockPurchase.getCommoName()));
		}
		if (StringUtil.isExist(stockPurchase.getPoNo())) {// 采购单名称
			detachedCriteria.add(Restrictions.like("poNo", stockPurchase.getPoNo()));
		}
		if (StringUtil.isExist(stockPurchase.getWhName())) {// 仓库名称
			detachedCriteria.add(Restrictions.like("whName", stockPurchase.getWhName()));
		}
		if (StringUtil.isExist(stockPurchase.getWhNo())) {// 仓库名称
			detachedCriteria.add(Restrictions.like("whNo", stockPurchase.getWhNo()));
		}
		return detachedCriteria;
	}

	@Override
	public boolean changeQuantity(StockPurchase stockPurchase) {
		return stockPurchaseDao.changeQuantity(stockPurchase) > 0;
	}
}